﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectSchedule.Entity;
using ProjectSchedule.Common;
using System.IO;
using System.Text.RegularExpressions;

namespace ProjectSchedule
{
    public partial class MainForm : Form
    {
        private OpenFileDialog ofd = new OpenFileDialog();
        private CTSchedule schedule=null;
        private XZSchedule schedulexz = null;
        private bool IsReSelect = false;
        private TWSchedule twschedule = null;

        public MainForm()
        {
            /*
            Logon nForm = new Logon();    //调用登录窗体
            if (nForm.ShowDialog() != DialogResult.OK)    //判断登录窗体返回值
            {
                Environment.Exit(Environment.ExitCode);    //退出程序
                return;
            }
            else
            {
                InitializeComponent();     //构造主窗体
            }
            */
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.btn_Search.Focus();
            this.tabControl1.SelectedIndex = 1;
        }

        // 浏览数据信息
        private void btn_Search_Click(object sender, EventArgs e)
        {         
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            this.txt_Path.Text = ofd.FileName;
            this.IsReSelect = true;
        }

        // 生成船体汇总
        private void btn_TotalCT_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_Path.Text.Trim()))
            {
                MessageBox.Show("请选择数据来源信息", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (IsFileInUse(txt_Path.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (this.IsReSelect || schedule == null)
            {
                schedule = Common.ExcelOperation.ReadExcel_CT(this.txt_Path.Text);
                if (schedule == null)
                {
                    MessageBox.Show("请选择正确的数据文件！", "提示");
                    return;
                }
            }
            var path = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成汇总(船体).xls");

            if (File.Exists(path) && IsFileInUse(path))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (ExcelOperation.SaveSummary_CT(path, schedule))
            {
                MessageBox.Show("生成 [船体汇总] 成功！", "提示");
            }
        }

        // 生成舾装汇总
        private void btn_TotalXZ_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_Path.Text.Trim()))
            {
                MessageBox.Show("请选择数据来源信息", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (IsFileInUse(txt_Path.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (this.IsReSelect || schedulexz == null)
            {
                schedulexz = Common.ExcelOperation.ReadExcel_XZ(this.txt_Path.Text);
                if (schedulexz == null)
                {
                    MessageBox.Show("请选择正确的数据文件！","提示");
                    return;
                }
            }
            var path = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成汇总(舾装).xls");
            if (File.Exists(path) && IsFileInUse(path))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (ExcelOperation.SaveSummary_XZ(path, schedulexz))
            {
                MessageBox.Show("生成 [舾装汇总] 成功！","提示");
            }
        }

        // 生成船体计划
        private void btn_PlanCT_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_Path.Text.Trim()))
            {
                MessageBox.Show("请选择数据来源信息", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (IsFileInUse(txt_Path.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (String.IsNullOrEmpty(this.cb_Year.Text.Trim()))
            {
                MessageBox.Show("请选择计划年信息", "提示");
                this.cb_Year.Focus();
                return;
            }
            if (String.IsNullOrEmpty(this.cb_Month.Text.Trim()))
            {
                MessageBox.Show("请选择计划月信息", "提示");
                this.cb_Month.Focus();
                return;
            }
            if (cb_Year.Text != "" && cb_Month.Text != "")
            {
                if (!IsYear(cb_Year.Text + "-" + cb_Month.Text + "-" + 01))
                {
                    MessageBox.Show("请输入正确的计划年或月份", "提示");
                    return;
                }
            }
            if (this.IsReSelect || schedule == null)
            {
                schedule = Common.ExcelOperation.ReadExcel_CT(this.txt_Path.Text);
                if (schedule == null)
                {
                    MessageBox.Show("请选择正确的数据文件！", "提示");
                    return;
                }
            }
            var path = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成计划(船体)_{0}-{1}.xls", cb_Year.Text, cb_Month.Text);

            if (File.Exists(path) && IsFileInUse(path))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (ExcelOperation.SaveCTPlan(path, schedule, Int32.Parse(cb_Year.Text), Int32.Parse(cb_Month.Text)))
            {
                MessageBox.Show("生成 [船体计划] 成功", "提示");
            }
        }

        // 生成舾装计划
        private void btn_PlanXZ_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_Path.Text.Trim()))
            {
                MessageBox.Show("请选择数据来源信息", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (IsFileInUse(txt_Path.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }
            if (String.IsNullOrEmpty(this.cb_Year.Text.Trim()))
            {
                MessageBox.Show("请选择计划年信息", "提示");
                this.cb_Year.Focus();
                return;
            }
            if (String.IsNullOrEmpty(this.cb_Month.Text.Trim()))
            {
                MessageBox.Show("请选择计划月信息", "提示");
                this.cb_Month.Focus();
                return;
            }
            if (cb_Year.Text != "" && cb_Month.Text != "")
            {
                if (!IsYear(cb_Year.Text + "-" + cb_Month.Text + "-" + 01))
                {
                    MessageBox.Show("请输入正确的计划年或月份", "提示");
                    return;
                }
            }
            if (this.IsReSelect || schedule == null)
            {
                schedulexz = Common.ExcelOperation.ReadExcel_XZ(this.txt_Path.Text);
                if (schedulexz == null)
                {
                    MessageBox.Show("请选择正确的数据文件！", "提示");
                    return;
                }
            }
            var sectionPath = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成计划(舾装_分段)_{0}-{1}.xls", cb_Year.Text, cb_Month.Text);
            var areaPath = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成计划(舾装_区域)_{0}-{1}.xls", cb_Year.Text, cb_Month.Text);
            var debugPath = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成计划(舾装_调试)_{0}-{1}.xls", cb_Year.Text, cb_Month.Text);

            if (File.Exists(sectionPath) && IsFileInUse(sectionPath))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (File.Exists(areaPath) && IsFileInUse(areaPath))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (File.Exists(debugPath) && IsFileInUse(debugPath))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            Boolean f1, f2, f3;
            f1 = ExcelOperation.SaveXZPlan_Section(sectionPath, schedulexz, Int32.Parse(cb_Year.Text), Int32.Parse(cb_Month.Text));
            f2 = ExcelOperation.SaveXZPlan_Area(areaPath, schedulexz, Int32.Parse(cb_Year.Text), Int32.Parse(cb_Month.Text));
            f3 = ExcelOperation.SaveXZPlan_Debug(debugPath, schedulexz, Int32.Parse(cb_Year.Text), Int32.Parse(cb_Month.Text));
            MessageBox.Show(String.Format("生成[舾装分段计划]{0}\r\n生成[舾装区域计划]{1}\r\n生成[舾装调试计划]{2}",
                f1?"成功":"失败",f2?"成功":"失败",f3?"成功":"失败"), "提示");
        }

        // 判断文件是否在使用
        public static bool IsFileInUse(string fileName)
        {
            bool inUse = true;
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,
                FileShare.None);
                inUse = false;
            }
            catch
            {
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            //True表示正在使用,False表示没有使用
            return inUse;
        }

        // 判断是否是正确的年月
        public bool IsYear(string year)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(year, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]"
                + @"|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|"
                + @"1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?"
                + @"2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468]"
                + @"[048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }

        // 胎位分段信息文件
        private void btn_SearchType_Click(object sender, EventArgs e)
        {
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            this.txt_PathType.Text = ofd.FileName;
            this.IsReSelect = true;
        }

        // 生成每个胎位匹配信息
        private void btn_Match_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_PathType.Text.Trim()))
            {
                MessageBox.Show("请选择胎位分段数据来源信息", "提示");
                this.btn_SearchType.Focus();
                return;
            }
            if (IsFileInUse(this.txt_PathType.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }

            if (this.IsReSelect || twschedule == null)
            {
                twschedule = Common.ExcelOperation.ReadExcel_TW(this.txt_PathType.Text);
                if (twschedule == null)
                {
                    MessageBox.Show("请选择胎位分段数据来源信息！", "提示");
                    return;
                }
            }

            if (String.IsNullOrEmpty(this.cb_YearType.Text.Trim()))
            {
                MessageBox.Show("请选择导出年信息", "提示");
                this.cb_YearType.Focus();
                return;
            }

            if (String.IsNullOrEmpty(this.cb_MonthType.Text.Trim()))
            {
                MessageBox.Show("请选择导出月信息", "提示");
                this.cb_MonthType.Focus();
                return;
            }

            if (cb_YearType.Text != "" && cb_MonthType.Text != "")
            {
                if (!IsYear(cb_YearType.Text + "-" + cb_MonthType.Text + "-" + 01))
                {
                    MessageBox.Show("请输入正确的计划年或月份", "提示");
                    return;
                }
            }

            // 保存文件
            var path = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成胎位流转信息_{0}-{1}.xls", cb_YearType.Text, cb_MonthType.Text);
            if (File.Exists(path) && IsFileInUse(path))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }

            if (ExcelOperation.SaveTWSectionArrange(path,twschedule,Int32.Parse(this.cb_YearType.Text),Int32.Parse(this.cb_MonthType.Text)))
            {
                MessageBox.Show("生成 [胎位流转信息] 成功！", "提示");
            }
        }

        private void btn_MatchAll_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_PathType.Text.Trim()))
            {
                MessageBox.Show("请选择胎位分段数据来源信息", "提示");
                this.btn_SearchType.Focus();
                return;
            }
            if (IsFileInUse(this.txt_PathType.Text.Trim()))
            {
                MessageBox.Show("该文件正在其他地方被使用,请先关闭其文件", "提示");
                this.btn_Search.Focus();
                return;
            }

            // 保存文件
            var path = ExcelOperation.CurrentDirectory + String.Format(@"\Export\生成全部胎位流转信息.xls");
            if (File.Exists(path) && IsFileInUse(path))
            {
                MessageBox.Show("请先关闭需保存的文件", "提示");
                return;
            }
            if (this.IsReSelect || twschedule == null)
            {
                twschedule = Common.ExcelOperation.ReadExcel_TW(this.txt_PathType.Text);
                if (twschedule == null)
                {
                    MessageBox.Show("请选择胎位分段数据来源信息！", "提示");
                    return;
                }


                if (ExcelOperation.SaveAllTWSectionArrange(path, twschedule))
                {
                    MessageBox.Show("生成 [全部胎位流转信息] 成功！", "提示");
                }
            }
        }
    }
}
